﻿using DevExpress.Utils;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Grid;

using DevTeamIT;
using System;
using System.IO;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Serialization;

namespace ConfExpert.Utility
{
    [Serializable]
    public class PrgSettings
    {
        public string POOzetColumns = "";

        public bool EnableAppearanceEvenRow = true;
        public DefaultBoolean AllowFormGlass = DefaultBoolean.False;
        public PopupFilterMode PopupFilterMode = PopupFilterMode.Contains;
        public EditorShowMode EditorShowMode = EditorShowMode.MouseDownFocused; //EditorShowMode.Click; idi
        public string AutoFilterCondition = "Contains";

        public string KstColor1 = "IndianRed";  //"OliveDrab";
        public string ZorunluAlanRengi = "Gold";
        public string SkinName = "Office 2013";   //"Office 2010 Black";

        public string StartupForm = null;
        public string StartupRibbonPage = null;

        private void SetEditorMode(Control Page)
        {
            foreach (Control cx in Page.Controls)
                if (cx is GridControl)
                {
                    if ((cx as GridControl).MainView is GridView)
                        ((cx as GridControl).MainView as GridView).OptionsBehavior.EditorShowMode = EditorShowMode;
                }
                else if (cx.Controls.Count > 0)
                    SetEditorMode(cx);
        }

        private void SetEvenRow(Control Page)
        {
            foreach (Control cx in Page.Controls)
                if (cx is GridControl)
                {
                    if ((cx as GridControl).MainView is GridView)
                        ((cx as GridControl).MainView as GridView).OptionsView.EnableAppearanceEvenRow = EnableAppearanceEvenRow;
                }
                else if (cx.Controls.Count > 0)
                    SetEvenRow(cx);
        }

        public void Apply()
        {
            PrgSettings settings = new PrgSettings();

            settings.StartupForm = this.StartupForm;
            settings.StartupRibbonPage = this.StartupRibbonPage;
            settings.PopupFilterMode = this.PopupFilterMode;
            settings.AutoFilterCondition = this.AutoFilterCondition;

            if (AppSession.PrgSettingsOrg.PopupFilterMode != this.PopupFilterMode)
                foreach (int rpval in Enum.GetValues(typeof(Rp)))
                    if (RepositoryManager.Get(rpval) is RepositoryItemGridLookUpEdit)
                        (RepositoryManager.Get(rpval) as RepositoryItemGridLookUpEdit).PopupFilterMode = PopupFilterMode;
                    else
                        (RepositoryManager.Get(rpval) as RepositoryItemSearchLookUpEdit).PopupFilterMode = PopupFilterMode;
            //if (Enum.GetName(typeof(Rp), rpval) != "V_Kumas")   //string to enum 


            settings.EditorShowMode = this.EditorShowMode;
            if (AppSession.PrgSettingsOrg.EditorShowMode != this.EditorShowMode)
                foreach (XtraForm form in AppSession.MainForm.MdiChildren)
                    SetEditorMode(form);


            settings.EnableAppearanceEvenRow = this.EnableAppearanceEvenRow;
            if (AppSession.PrgSettingsOrg.EnableAppearanceEvenRow != this.EnableAppearanceEvenRow)
                foreach (XtraForm form in AppSession.MainForm.MdiChildren)
                    SetEvenRow(form);

            //settings.SkinName = this.SkinName;
            settings.ZorunluAlanRengi = this.ZorunluAlanRengi;

            //if (AppSession.PrgSettingsOrg.KstColor1 != this.KstColor1)
            //    foreach (XtraForm form in AppSession.MainForm.MdiChildren)
            //        if (form.Name == "KSip")
            //            ((GridControl)(form.Controls.Find("gcKst", true)[0])).ViewCollection[0].RefreshData();  //(BandedGridView) bgvKst
            settings.KstColor1 = this.KstColor1;

            //if (AppSession.PrgSettingsOrg.KstFixed != this.KstFixed)
            //    foreach (XtraForm form in AppSession.MainForm.MdiChildren)
            //        if (form.Name == "KSip")
            //            ((BandedGridView)((GridControl)(form.Controls.Find("gcKst", true)[0])).ViewCollection[0]).Bands[0].Fixed = this.KstFixed ? FixedStyle.Left : FixedStyle.None;
            //settings.KstFixed = this.KstFixed;

            if (AppSession.PrgSettingsOrg.AllowFormGlass != this.AllowFormGlass)
                AppSession.MainForm.AllowFormGlass = this.AllowFormGlass;
            settings.AllowFormGlass = this.AllowFormGlass;




            AppSession.PrgSettingsOrg = settings;
        }

        public static PrgSettings LoadSettings(string xmlSettings)
        {
            if (xmlSettings.Trim() == string.Empty)
                return new PrgSettings();

            byte[] buffer = System.Text.Encoding.GetEncoding("ISO-8859-9").GetBytes(xmlSettings);
            MemoryStream fs = new MemoryStream(buffer);
            XmlReader reader = XmlReader.Create(fs);
            XmlSerializer serializer = new XmlSerializer(typeof(PrgSettings));
            PrgSettings oSettings = (PrgSettings)serializer.Deserialize(reader);
            fs.Close();

            return oSettings;
        }

        public void Save()
        {
            XmlSerializer serializer = new XmlSerializer(typeof(PrgSettings));

            MemoryStream ms = new MemoryStream();
            serializer.Serialize(ms, this);
            ms.Seek(0, SeekOrigin.Begin);
            byte[] bytes = new byte[ms.Length];
            ms.Read(bytes, 0, (int)ms.Length);
            string xml = System.Text.Encoding.GetEncoding("ISO-8859-9").GetString(bytes);

            Session.DataManager.ExecuteNonQuery("UpdUserSettings", Session.CurrentUser.UserId, xml);

            Apply();
        }


    }
}